<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>垂直分步滚动动画 - JS版本</title>
    <style>
      .container {
        width: 500px;
        height: 400px;
        margin: 100px auto;
        overflow: hidden;
        border: 1px solid #ccc;
      }

      .box {
        /* 移除CSS动画，改用JS实现 */
      }

      .box-item {
        display: flex;
        flex-direction: column;
      }

      .item {
        height: 100px;
        background-color: red;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        color: white;
      }

      .item:nth-child(2) {
        background-color: blue;
      }

      .item:nth-child(3) {
        background-color: green;
      }

      .item:nth-child(4) {
        background-color: orange;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="box" id="animatedBox">
        <div class="box-item">
          <div class="item">1</div>
          <div class="item">2</div>
          <div class="item">3</div>
          <div class="item">4</div>
        </div>
        <div class="box-item">
          <div class="item">1</div>
          <div class="item">2</div>
          <div class="item">3</div>
          <div class="item">4</div>
        </div>
      </div>
    </div>

    <script>
      // 获取需要动画的元素
      const box = document.getElementById('animatedBox');

      // 定义关键帧
      const keyframes = [
        { transform: 'translateY(0)' }, // 0%
        { transform: 'translateY(0)' }, // 16.67%
        { transform: 'translateY(-100px)' }, // 25%
        { transform: 'translateY(-100px)' }, // 41.67%
        { transform: 'translateY(-200px)' }, // 50%
        { transform: 'translateY(-200px)' }, // 66.67%
        { transform: 'translateY(-300px)' }, // 75%
        { transform: 'translateY(-300px)' }, // 91.67%
        { transform: 'translateY(-400px)' }, // 100%
      ];

      // 定义时间线
      const keyframeOffsets = [0, 16.67 / 100, 25 / 100, 41.67 / 100, 50 / 100, 66.67 / 100, 75 / 100, 91.67 / 100, 1];

      // 组合关键帧和时间线
      const finalKeyframes = keyframes.map((frame, index) => {
        return {
          ...frame,
          offset: keyframeOffsets[index],
        };
      });

      // 定义动画选项
      const options = {
        duration: 6000, // 6秒
        iterations: Infinity, // 无限循环
      };

      // 应用动画
      const animation = box.animate(finalKeyframes, options);
    </script>
  </body>
</html>
